package com.shenfc.fxzj.action;

//import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

import com.shenfc.fxzj.config.ConfigBean;
import com.shenfc.fxzj.config.User;
import com.shenfc.fxzj.domain.Admin;
import com.shenfc.fxzj.domain.SysUser;
import com.shenfc.fxzj.mapper.AdminMapper;
import com.shenfc.fxzj.mapper.SysUserMapper;
import com.shenfc.fxzj.task.AsyncTask;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;
import java.util.concurrent.Future;

//import com.shenfc.fxzj.service.ISchedualServiceHi;
//import org.springframework.amqp.rabbit.core.RabbitTemplate;
//import org.springframework.web.client.RestTemplate;

/**
 * 作者：沈飞超
 * 功能描述：
 * 创建日期：2018/3/29 11:11
 */
@Api(value = "HelloWorldAction", description = "HelloWorld控制层")
@Controller
@Slf4j
public class HelloWorldAction {

    @Autowired
    private ConfigBean configBean;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private User user;

    @Autowired
    private StringRedisTemplate redisTemplate;

//    @Autowired
//    private RabbitTemplate rabbitTemplate;

    @Autowired
    private AsyncTask asyncTask;

//    @Autowired
//    RestTemplate restTemplate;

//    @Autowired
//    ISchedualServiceHi schedualServiceHi;

    @ApiIgnore//使用该注解忽略这个API
    @RequestMapping("/hello")
    @ResponseBody
    public String hello() {
        log.info("测试日志信息");
        return "Hello SpringBoo1t!111";
    }

    @ApiIgnore//使用该注解忽略这个API
    @RequestMapping(value = "/lucy")
    public String miya() {
        return configBean.getGreeting() + " >>>>" + configBean.getName() + " >>>>" + configBean.getUuid() + " >>>>" + configBean.getMax();
    }

    @ApiOperation(value = "查询用户", notes = "查询用户测试接口", httpMethod = "POST")
    @RequestMapping(value = "/user")
    public List<SysUser> user(@ApiParam(value = "用户名", name = "username", required = true) String username, @ApiParam(name = "user", value = "用户", required = true) @RequestBody User user) {
        List<SysUser> sysUsers = sysUserMapper.selectAllUser();
        return sysUsers;
    }

    @ApiOperation(value = "redis订阅发布", notes = "redis订阅发布", httpMethod = "GET")
    @RequestMapping(value = "/redis")
    public void redis(@ApiParam(value = "消息", name = "message", required = true) String message) {
        redisTemplate.convertAndSend("phone", message);
        System.out.println("redis订阅发布测试结束");
    }

//    @ApiOperation(value = "rabbitMqTopic模糊匹配队列", notes = "rabbitMqTopic模糊匹配队列", httpMethod = "GET")
//    @RequestMapping(value = "/rabbitMq")
//    public void rabbitMqTopic(@ApiParam( value = "消息", name = "message",required = true) String message) {
//        rabbitTemplate.convertAndSend("spring-boot-exchange","rabbitKey1",message);
//        System.out.println("rabbitMqTopic模糊匹配队列测试结束");
//    }
//
//    @ApiOperation(value = "rabbitMqFanout订阅发布", notes = "rabbitMqFanout订阅发布", httpMethod = "GET")
//    @RequestMapping(value = "/rabbitMqFanout")
//    public void rabbitMqFanout(@ApiParam( value = "消息", name = "message",required = true) String message) {
//        rabbitTemplate.convertAndSend("fanoutExchange","",message);
//        System.out.println("rabbitMqFanout订阅发布测试结束");
//    }

    @ApiOperation(value = "验证表单", notes = "验证表单", httpMethod = "GET")
    @RequestMapping(value = "/validateForm")
    public void validateForm(@ApiParam(value = "用户", name = "sysUser", required = true) @Valid SysUser sysUser, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            System.out.println(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        System.out.println(sysUser);
    }

    @ApiOperation(value = "异步调用", notes = "异步调用", httpMethod = "GET")
    @RequestMapping("/doAsyncTask")
    public String doAsyncTask() throws Exception {
        long currentTimeMillis = System.currentTimeMillis();

        Future<String> task1 = asyncTask.task1();
        Future<String> task2 = asyncTask.task2();
        Future<String> task3 = asyncTask.task3();
        String result = null;
        while (!(task1.isDone() && task2.isDone() && task3.isDone())) {
            Thread.sleep(10); //10-millisecond pause between each check
        }
        long currentTimeMillis1 = System.currentTimeMillis();
        System.out.println("--> " + task1.get());
        System.out.println("--> " + task2.get());
        System.out.println("--> " + task3.get());
        result = "task任务总耗时:" + (currentTimeMillis1 - currentTimeMillis) + "ms";
        return result;
    }

    @ApiOperation(value = "thymeleaf视图", notes = "thymeleaf视图", httpMethod = "GET")
    @RequestMapping("/thymeleaf")
    public String thymeleaf() throws Exception {
        return "test";
    }

//    @ApiOperation(value = "ribbon负载均衡", notes = "ribbon负载均衡", httpMethod = "GET")
//    @RequestMapping("/ribbon")
//    @HystrixCommand(fallbackMethod = "hiError")
//    public String ribbon(String name) throws Exception{
//        return restTemplate.getForObject("http://SERVICE-HI/hi?name="+name,String.class);
//    }
//
//    @ApiOperation(value = "feign负载均衡", notes = "feign负载均衡", httpMethod = "GET")
//    @RequestMapping(value = "/feign",method = RequestMethod.GET)
//    public String sayHi(@RequestParam String name){
//        return schedualServiceHi.sayHiFromClientOne(name);
//    }

    public String hiError(String name) {
        return "hi," + name + ",sorry,error!";
    }

    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public String test(@RequestParam String name) {
        Admin admin = adminMapper.selectByPrimaryKey(1L);
        return "123";
    }
}